
package kaizone.haining.chaobohui.json;

import java.util.HashMap;

import kaizone.haining.chaobohui.util.Utils;

import org.json.JSONObject;

import android.text.TextUtils;

public class VersionDO extends JSONResponse {

    public static final String IOS = "ios";

    public static final String ANDROID = "android";

    public static final String WINDOWSPHONE = "windowsphone";

    public static final String VERSION = "version";

    public static final String URL = "url";

    private HashMap<String, Info> data;

    public VersionDO() {

    }

    public String getVersion(String platforms) {
        if (data == null || data.isEmpty())
            return null;
        Info info = data.get(platforms);
        if (info == null)
            return null;
        return info.version;
    }

    public int getVersion1(String platforms) {
        String version = getVersion(platforms);
        if (TextUtils.isEmpty(version))
            return 0;
        if (!Utils.isNumeric(version))
            return 0;
        int versionint = 1;
        try {
            versionint = Integer.valueOf(version);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionint;
    }

    public HashMap<String, Info> getData() {
        return data;
    }

    public void setData(HashMap<String, Info> data) {
        this.data = data;
    }

    public static VersionDO parseJson(String str) {
        VersionDO versionDO = new VersionDO();
        try {
            JSONObject jsonObject = new JSONObject(str);

            JSONResponse response = JSONResponse.parse(jsonObject);
            versionDO.setJSONResponse(response);

            JSONObject jsondata = jsonObject.optJSONObject(JSONResponse.DATA);
            if (jsondata != null) {
                HashMap<String, Info> data = new HashMap<String, VersionDO.Info>();
                JSONObject iosJson = jsondata.optJSONObject(IOS);
                Info iosInfo = Info.parse(iosJson);
                data.put(IOS, iosInfo);

                JSONObject androidJson = jsondata.optJSONObject(ANDROID);
                Info androidInfo = Info.parse(androidJson);
                data.put(ANDROID, androidInfo);

                JSONObject wpJson = jsondata.optJSONObject(WINDOWSPHONE);
                Info wpInfo = Info.parse(wpJson);
                data.put(WINDOWSPHONE, wpInfo);

                versionDO.setData(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return versionDO;
    }

    public static class Info {
        public String name;

        public String version;

        public String url;

        public static Info parse(JSONObject jsonObject) {
            Info info = new Info();
            info.version = jsonObject.optString(VERSION);
            info.url = jsonObject.optString(URL);
            return info;
        }
    }

}
